package com.nuoniu.sibanyun.mapper.erp;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.entity.bo.*;
import com.nuoniu.sibanyun.entity.dto.DynamicVoucherDto;
import com.nuoniu.sibanyun.entity.erp.ErpVoucherDesign;
import com.nuoniu.sibanyun.entity.erp.ErpVoucherTable;
import com.nuoniu.sibanyun.entity.erp.barcode.ErpBarcodeMainInfo;
import com.nuoniu.sibanyun.entity.vo.VoucherReferenceVo;
import com.nuoniu.sibanyun.mapper.provider.VoucherDesignDynamicProvider;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 单据设计 Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-23
 */
public interface ErpVoucherDesignMapper extends BaseMapper<ErpVoucherDesign> {

    /**
     * 最新ID
     * @param tableName
     * @param companyId
     * @return
     */
    Integer getLastId(String tableName, Integer companyId, Integer accountId,@Param("dto") DynamicVoucherDetailBo dynamicVoucherDetailBo);


    /**
     * 第一ID
     * @param tableName
     * @param companyId
     * @return
     */
    Integer getFirstId(String tableName,Integer companyId,Integer accountId,@Param("dto") DynamicVoucherDetailBo dynamicVoucherDetailBo);

    /**
     * 上一个ID
     * @param tableName
     * @param companyId
     * @param currentId
     * @return
     */
    Integer getPreId(String tableName,Integer companyId,Integer accountId,Integer currentId,@Param("dto") DynamicVoucherDetailBo dynamicVoucherDetailBo);

    /**
     * 下一个ID
     * @param tableName
     * @param companyId
     * @param currentId
     * @return
     */
    Integer getNextId(String tableName,Integer companyId,Integer accountId,Integer currentId,@Param("dto") DynamicVoucherDetailBo dynamicVoucherDetailBo);
    /**
     * 检查当前租户的自定义字段 是否有使用
     * @param id
     * @return
     */
    @Select(" SELECT COUNT(1) FROM ${tableName} tab  WHERE tab.company_id = #{companyId} AND tab.define_fields->'$.\"${id}\"' ")
    int countDefineFieldUsedNumber(String tableName,Integer companyId,Integer id);

    /**
     * 检查 当前 自定义字段 是否 被引用
     * @param companyId
     * @param id
     * @return
     */
    @Select(" SELECT COUNT(1) FROM pub_voucher_design tab  WHERE tab.company_id = #{companyId} AND tab.reference_design_id = #{id} ")
    int countReferenceFieldUsedNumber(Integer companyId,Integer id);

    /**
     * 初始化 单据设计（租户首次建立账套触发）
     * @param accountId
     * @param companyId
     * @param companyName
     * @param createUserId
     * @param createUserName
     * @param createDate
     */
    @Insert("INSERT INTO pub_voucher_design ( account_id, company_id, company_name, sys_name, name, code, source, column_type, val, length, sort, data_tab, data_type, is_summary, on_off, on_fill, on_edit, on_export, is_search, on_locked, column_width, status, create_user_id, create_user_name, create_date) \n" +
            "SELECT #{accountId}, #{companyId}, #{companyName}, sys_name, name, code, source, column_type, val, length, sort, data_tab, data_type, is_summary, on_off, on_fill, on_edit, on_export, is_search, on_locked, column_width, status, #{createUserId}, #{createUserName}, #{createDate} \n" +
            "FROM pub_voucher_design WHERE company_id = 1 AND account_id = 0")
    void initDefErpVoucherDesign(Integer accountId, Integer companyId, String companyName, Integer createUserId, String createUserName, LocalDateTime createDate);

    /**
     * 单据动态分页
     * @param dynamicVoucherDto
     * @param paramMap
     * @param page
     * @return
     */
    @SelectProvider(type = VoucherDesignDynamicProvider.class, method = "voucherDynamicSql")
    Page<HashMap<String,Object>> voucherDynamicPage(Integer currentAccountId,DynamicVoucherDto dynamicVoucherDto , ErpVoucherTable erpVoucherTable, HashMap<String,Object> paramMap, Page page);

    /**
     * 单据主表详情（动态查询）
     * @param designSearchDtos
     * @param erpVoucherTable
     * @param id
     * @return
     */
    @SelectProvider(type = VoucherDesignDynamicProvider.class, method = "voucherDynamicByIdSql")
    HashMap<String,Object> voucherDynamicById(Integer currentAccountId,List<ErpVoucherDesign> designSearchDtos, ErpVoucherTable erpVoucherTable, Integer id);

    /**
     * 单据明细表列表（动态查询）
     * @param designSearchDtos
     * @param erpVoucherTable
     * @param ids
     * @return
     */
    @SelectProvider(type = VoucherDesignDynamicProvider.class, method = "voucherDynamicDetailListByIdSql")
    List<HashMap<String,Object>> voucherDynamicDetailListByIds(Integer currentAccountId,List<ErpVoucherDesign> designSearchDtos, DynamicVoucherDto dynamicVoucherDto,ErpVoucherTable erpVoucherTable, String ids);

    /**
     *
     * @param erpVoucherDesignList
     * @param erpVoucherTable
     * @param detailIds
     * @return
     */
    @SelectProvider(type = VoucherDesignDynamicProvider.class, method = "voucherDynamicSpecifyFieldRecordSql")
    List<HashMap<String, Object>> getSpecifyFieldRecord(List<ErpVoucherDesign> erpVoucherDesignList,ErpVoucherTable erpVoucherTable, List<Integer> detailIds);

    /**
     * 查询 组装条码档案
     * @param erpVoucherTable
     * @return
     */
    @SelectProvider(type = VoucherDesignDynamicProvider.class, method = "listNyVoucherTableSql")
    List<ErpBarcodeMainInfo> listByVoucherTable(ErpVoucherTable erpVoucherTable, List<Integer> detailIds);


    /**
     * 系统管理员 更改租户的单据设计字段的锁定状态
     * @param erpVoucherDesign
     * @return
     */
    boolean batchUpdateCompanyErpVoucherDesign(@Param("dto") ErpVoucherDesign erpVoucherDesign);

    /**
     * 查询 单据 主表（计算 累计执行数量）
     * @param tableName
     * @param companyId
     * @param accountId
     * @param id
     * @return
     */
    VoucherMainBo getVoucherMainBoById(String tableName,Integer companyId,Integer accountId,Integer id,boolean isGetRet);

    /**
     * 查询 单据 主表（计算 累计发票数量）
     * @param tableName
     * @param companyId
     * @param accountId
     * @param id
     * @return
     */
    VoucherInvoiceMainBo getVoucherInvoiceMainBoById(String tableName,Integer companyId,Integer accountId,Integer id);

    /**
     * 查询 单据 主表（计算 累计退货数量）
     * @param tableName
     * @param companyId
     * @param accountId
     * @param id
     * @return
     */
    VoucherMainRetBo getVoucherMainRetBoById(String tableName, Integer companyId, Integer accountId, Integer id);

    /**
     * 查询 单据 明细表（计算 累计执行数量）
     * @param tableName
     * @param companyId
     * @param mainId
     * @return
     */
    List<VoucherDetailBo> getVoucherDetailBoByMainId(String tableName,Integer companyId,Integer mainId,boolean isGetRet);

    /**
     * 查询 单据 明细表（计算 累计退货数量）
     * @param tableName
     * @param companyId
     * @param mainId
     * @return
     */
    List<VoucherDetailRetBo> getVoucherDetailRetBoByMainId(String tableName, Integer companyId, Integer mainId);

    /**
     * 查询 单据 明细表（计算 累计开票数量）
     * @param tableName
     * @param companyId
     * @param mainId
     * @return
     */
    List<VoucherInvoiceDetailBo> getVoucherInvoiceDetailBoByMainId(String tableName, Integer companyId, Integer mainId);

    /**
     * 更新来源单据 （累计执行情况）
     * @param calculateSourceVoucherUpdateBo
     * @return
     */
    boolean updateSourceVoucherMainCumExecuteQuantity(@Param("bo") CalculateSourceVoucherUpdateBo calculateSourceVoucherUpdateBo);


    /**
     * 更新来源单据的开票数量
     * @param calculateSourceVoucherUpdateBo
     * @return
     */
    boolean updateSourceVoucherMainInvoiceCumQuantity(@Param("bo") CalculateSourceVoucherUpdateBo calculateSourceVoucherUpdateBo);

    /**
     * 更新来源单据 （累计退货数量）
     * @param tableName
     * @param companyId
     * @param accountId
     * @param id
     * @param cumRetQuantity
     * @return
     */
    boolean updateSourceVoucherMainCumRetQuantity(String tableName, Integer companyId, Integer accountId, Integer id, BigDecimal cumRetQuantity);

    /**
     * 批量更新 来源单据明细
     * @param tableName
     * @param companyId
     * @param accountId
     * @param voucherDetailBos
     * @return
     */
    boolean updateBatchSourceVoucherDetail(String tableName, Integer companyId, Integer accountId,@Param("set") Collection<VoucherDetailBo> voucherDetailBos);


    /**
     * 批量更新 来源单据明细（发票）
     * @param tableName
     * @param companyId
     * @param accountId
     * @param voucherInvoiceDetailBos
     * @return
     */
    boolean updateBatchSourceInvoiceVoucherDetail(String tableName, Integer companyId, Integer accountId,@Param("set") Collection<VoucherInvoiceDetailBo> voucherInvoiceDetailBos);

    /**
     * 批量更新 来源单据
     * @param tableName
     * @param companyId
     * @param accountId
     * @param voucherDetailRetBos
     * @return
     */
    boolean updateBatchSourceVoucherDetailRet(String tableName, Integer companyId, Integer accountId,@Param("set") Collection<VoucherDetailRetBo> voucherDetailRetBos);




    List<VoucherReferenceVo> listByReferenceTableCode(Integer companyId, Integer accountId, String voucherCode);

}
